//
//  XDLoginTextFieldCell.m
//  XDEducation
//
//  Created by apple on 2017/4/6.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDLoginTextFieldCell.h"
#import "LLCountDownButton.h"

@interface XDLoginTextFieldCell ()

/** 图片 */
@property(nonatomic,strong) UIImageView *iconImageView;

/** 标题 */
@property(nonatomic,strong) UILabel *titleLabel;



/** 底线 */
@property(nonatomic,strong) UIImageView *lineImageView;



/** 发送按钮验证码 */
@property(nonatomic,strong) LLCountDownButton *countButton;

@end

static NSString *const cellId = @"cell";

@implementation XDLoginTextFieldCell


+(instancetype)cellImageName:(NSString *)imageName title:(NSString *)title textFieldPalceHolder:(NSString *)placeHolder tableView:(UITableView *)tableView{

    XDLoginTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        cell = [[XDLoginTextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId ImageName:imageName title:title textFieldPalceHolder:placeHolder];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier ImageName:(NSString *)imageName title:(NSString *)title textFieldPalceHolder:(NSString *)placeHolder{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
      
        /** 图片 */
        _iconImageView = [[UIImageView alloc]init];
        _iconImageView.image = [UIImage imageNamed:imageName];
        [self.contentView addSubview:_iconImageView];
        
        /** 标题 */
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = title;
        _titleLabel.textColor = UIColorFromRGB(0x4C4C4C);
        _titleLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:_titleLabel];
        
        /** textField */
        _textField = [[UITextField alloc]init];
        _textField.placeholder = placeHolder;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.borderStyle = UITextBorderStyleNone;
        [self.contentView addSubview:_textField];
        
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc]initWithString:placeHolder];
        [placeholder addAttribute:NSForegroundColorAttributeName
                           value:UIColorFromRGB(0xcccccc)
                           range:NSMakeRange(0, placeHolder.length)];
        
        [placeholder addAttribute:NSFontAttributeName
                           value:[UIFont boldSystemFontOfSize:15]
                           range:NSMakeRange(0, placeHolder.length)];
        _textField.attributedPlaceholder = placeholder;
        
        /** 底线 */
        _lineImageView = [[UIImageView alloc]init];
        _lineImageView.image = [UIImage imageNamed:@"login_seg_defult"];
        [self.contentView addSubview:_lineImageView];
        
        
        /** 验证码倒计时button */
        //需要验证码的时候添加上去
        _countButton = [[LLCountDownButton alloc]init];
        _countButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_countButton setBackgroundImage:[UIImage imageNamed:@"register_btn_send_message_default"] forState:UIControlStateNormal];
        [_countButton setBackgroundImage:[UIImage imageNamed:@"register_btn_send_message_pressed"] forState:UIControlStateDisabled];
        _countButton.duration = 60;
        [_countButton addTarget:self action:@selector(countButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_countButton];

        
    }
    return self;
}


/**
 设置frame
 */
-(void)layoutSubviews{

    [super layoutSubviews];
    
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(16, 18));
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconImageView.mas_right).offset(18);
        make.centerY.equalTo(_iconImageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(60, 20));
    }];
    


    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel.mas_right).offset(10);
        make.centerY.equalTo(_iconImageView.mas_centerY);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.height.equalTo(self.contentView.mas_height).offset(-20);
        //make.height.mas_equalTo(30);
    }];
        

    [_lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-1);
        make.height.mas_equalTo(1);
    }];
}

-(void)setHidePassword:(BOOL)hidePassword{
    
    _textField.secureTextEntry = hidePassword;

}

-(void)setIsIdentityCodeType:(BOOL)isIdentityCodeType{

    if (isIdentityCodeType) {

        
        
        [_countButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView.mas_right).offset(-15);
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(100, 40));
        }];
        
        [_textField removeConstraints:_textField.constraints];
        
        [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_titleLabel.mas_right).offset(10);
            make.centerY.equalTo(_iconImageView.mas_centerY);
            make.height.equalTo(self.contentView.mas_height).offset(-20);
        }];
        
    }
}


#pragma - mark  TextField 代理方法




-(void)countButtonClicked{

    if (self.identityCodeBlock) {
        self.identityCodeBlock();
    }
}
@end
